package com.ruoyi.web.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysUnit;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysCourse;
import com.ruoyi.system.service.ISysUnitService;

/**
 * 单元管理 控制层
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/unit")
public class SysUnitController extends BaseController
{
    @Autowired
    private ISysUnitService sysUnitService;

    /**
     * 获取列表
     */
    @PreAuthorize("@ss.hasPermi('system:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUnit sysUnit)
    {
        startPage();
        List<SysUnit> list = sysUnitService.selectList(sysUnit);
        return getDataTable(list);
    }

    /**
     * 根据ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:unit:query')")
    @GetMapping(value = "/{unitId}")
    public AjaxResult getInfo(@PathVariable Long unitId)
    {
        return success(sysUnitService.selectUnitById(unitId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:unit:add')")
    @Log(title = "单元管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysUnit sysUnit)
    {

        sysUnit.setCreateBy(getUsername());
        int rows = sysUnitService.insert(sysUnit);
        return toAjax(rows);
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('system:unit:edit')")
    @Log(title = "单元管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysUnit sysUnit)
    {
        sysUnit.setUpdateBy(getUsername());
        return toAjax(sysUnitService.update(sysUnit));
    }

    /**
     * 删除教师
     */
    @PreAuthorize("@ss.hasPermi('system:unit:remove')")
    @Log(title = "单元管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{unitIds}")
    public AjaxResult remove(@PathVariable Long[] unitIds)
    {
        return toAjax(sysUnitService.deleteByIds(unitIds));
    }

    /**
     * 获取册本列表
     */
    @PreAuthorize("@ss.hasPermi('system:unit:list')")
    @GetMapping("/courseTree")
    public AjaxResult courseTree(SysCourse sysCourse)
    {
        return success(sysUnitService.courseTreeList(sysCourse));
    }

//    /**
//     * 绑定单词
//     */
//    @PreAuthorize("@ss.hasPermi('system:unit:bind')")
//    @Log(title = "单元管理", businessType = BusinessType.UPDATE)
//    @PostMapping("/{teacherId}/bind-class/{classIds}")
//    public AjaxResult bindClass(@PathVariable Long teacherId, @PathVariable Long[] classIds)
//    {
//        sysUnitService.insertTeacherClass(teacherId, classIds);
//        return success();
//    }

//    /**
//     * 解绑单词
//     */
//    @PreAuthorize("@ss.hasPermi('system:unit:unbind')")
//    @Log(title = "单元管理", businessType = BusinessType.UPDATE)
//    @DeleteMapping("/{teacherId}/unbind-class/{classId}")
//    public AjaxResult unbindClass(@PathVariable Long teacherId, @PathVariable Long classId)
//    {
//        sysUnitService.deleteTeacherClassInfo(teacherId, classId);
//        return success();
//    }
}